import inspect
import os
import glob
import sys
import openpyxl
from openpyxl import load_workbook
from .ExcelAPI_Utils import *



def get_infos_from_excels(file_paths : list[str]):
    
    excels_dic = {}
    svd_info = {}
    
    # 读取excel表的sheet信息
    sheet_dic = read_sheetinfos(file_paths)  
    if sheet_dic is None :
        sys.exit(2)
    
    excels_dic["sheet_dic"] = sheet_dic
    
    #获取DEVICE信息
    info_of_device = read_key_and_value_pairs(sheet_dic['DEVICE'],valid_header_DEVICE)
    if info_of_device is None :
        print(f"[Error]Fail to Get DEVICE info!!! \n")
        sys.exit(2)
        
    svd_info["device"] = info_of_device
    
    #获取CPU信息
    info_of_cpu = read_key_and_value_pairs(sheet_dic['CPU'],valid_header_CPU)
    if info_of_cpu is None :
        print(f"[Error]Fail to Get CPU info!!! \n")
        sys.exit(2)
        
    svd_info["cpu"] = info_of_cpu
    
    #获取外设的信息表
    peripherals_overview_infos = get_peripheral_overviews(sheet_dic['PERIPHERALS'])
    if peripherals_overview_infos is None :
        print(f"[Error]Fail to Get PERIPHERALS overview info!!! \n")
        sys.exit(2)
        
    # 给后续脚本一个flag，方便调试xlxs，允许只写map不写detail sheet
    for peripheral_name in peripherals_overview_infos:
        peripherals_overview_infos[peripheral_name]["has_registers"] = False
    
    svd_info["peripherals_dic"] = {}
    svd_info["peripherals_dic"]["overview"] = peripherals_overview_infos
        
    #获取外设别名,IC的名称是别名,SVD的是正式名称,后面操作会在脚本里面把别名替换成正式名称
    peripheral_alias = get_peripheral_alias(peripherals_overview_infos)
    if peripheral_alias is None :
        print(f"[Error]Fail to Get PERIPHERALS alias!!! \n")
        sys.exit(2)
        
    svd_info["peripherals_dic"]["alias"] = peripheral_alias
    
    #获取外设的headerStructNameGroup,对于属于同一个headerStructNameGroup的外设,后面的操作会在SVD文件里面详细其中第一个外设,其余外设会用derivedFrom
    peripheral_headerStructNameGroup = get_peripheral_headerStructNameGroup(peripherals_overview_infos)
    if peripheral_headerStructNameGroup is None :
        print("Get No PERIPHERALS headerStructNameGroup!!! \n")
        
    svd_info["peripherals_dic"]["headerStructNameGroup"] = peripheral_headerStructNameGroup
    
    #获取外设的信息页数据
    sheet_dics_of_peripherals = get_peripher_sheetinfos(sheet_dic)
    if sheet_dics_of_peripherals is None :
        print("Get No PERIPHERALS!!! \n")
        sys.exit(2)
    excels_dic["sheet_dics_of_peripherals"] = sheet_dics_of_peripherals
    
    #基于info_of_peripherals获取所有外设的信息
    if False :
        try :
            info_of_peripheral_regs = read_peripherals_registers_info(sheet_dics_of_peripherals)
        except Exception as e:
            print("read_peripherals_registers_info ERROR!!! \n")
            sys.exit(2)
    else:
        info_of_peripheral_regs = read_peripherals_registers_info(sheet_dics_of_peripherals)
        
        
        
    if info_of_peripheral_regs is None :
        print("Get No info_of_peripheral_regs!!! \n")
        sys.exit(2)
    
    peripherals_info = get_peripherals_info(peripherals_overview_infos, info_of_peripheral_regs,peripheral_alias)
    if peripherals_info is None :
        print("Get No peripherals_info!!! \n")
        sys.exit(2)
    svd_info["peripherals"] = peripherals_info
    
    return svd_info
        
        